<?php

/**
 * This is the model class for table "comments".
 *
 * The followings are the available columns in table 'comments':
 * @property integer $id
 * @property integer $languageId
 * @property integer $userId
 * @property string $name
 * @property string $email
 * @property string $value
 * @property string $createdAt
 * @property string $updatedAt
 */
class Comment extends ActiveRecord {

	public $reCaptcha;

	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Comment the static model class
	 */
	public static function model($className = __CLASS__) {
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName() {
		return 'comments';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules() {
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('value', 'required'),
			array('name', 'required', 'on'=>'insertGuest'),
			array('name', 'length', 'max'=>255),
			array('email', 'length', 'max'=>128),
			array(
				'reCaptcha',
				'EReCaptchaValidator',
				'privateKey'=>Yii::app()->params['reCaptcha']['key']['private'],
				'on'=>'insertGuest',
			),
			array('name, email, value', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations() {
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'language'=>array(self::BELONGS_TO, 'Language', 'languageId'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels() {
		return array(
			'id'=>'ID',
			'languageId'=>'Language',
			'userId'=>'User',
			'name'=>'Name',
			'email'=>'Email',
			'value'=>'Comment',
			'reCaptcha'=>'Human Verification',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search() {
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria = new CDbCriteria;

		$criteria->compare('languageId', $this->languageId);
		$criteria->compare('userId', $this->userId);
		$criteria->compare('name', $this->name, true);
		$criteria->compare('email', $this->email, true);
		$criteria->compare('value', $this->value, true);

		return new CActiveDataProvider($this, array('criteria'=>$criteria));
	}

	public function getCommentatorName() {
		if ($this->userId) {
			$params = array(':user_id'=>$this->userId);
			$user = Profile::model()->find('user_id = :user_id', $params);
			if ($user) {
				return $user->firstname ? $user->firstname : 'Anonymous';
			}
		} else {
			return $this->name ? $this->name : 'Anonymous';
		}
	}

	public function getCommentatorEmail() {
		if ($this->userId) {
			$params = array(':id'=>$this->userId);
			$user = User::model()->find('id = :id' , $params);
			if ($user) {
				return $user->email ? $user->email : '';
			}
		} else {
			return $this->email ? $this->email : '';
		}
	}

}